package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;

import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author qzh
 * @create 2020-02-01 2020/2/1
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Reference
    private UserService userService;

    @RequestMapping("/getUsername")
    public Result getUsername(){

        org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        System.out.println(user);
        if(user!=null){
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);


    }

    @RequestMapping("/personIsFine")
    public Result personIsFine(){
        try {
            List<String> setmealNames = new ArrayList<>();
            List<Map<String,Object>> setmealCount = userService.findIsFineCount();
            for (Map<String, Object> map : setmealCount) {
                String name = (String) map.get("name");
                setmealNames.add(name);
            }
            Map<String,Object> data = new HashMap<>();

            data.put("setmealNames",setmealNames);
            data.put("setmealCount",setmealCount);

            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS);
        }

    }


    //2.添加用户信息
    @RequestMapping("/add")
    public Result add(@RequestBody User user, Integer[] roleIds){
        try {
            userService.add(user,roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加用户信息失败");
        }
        return new Result(true,"添加用户信息成功");
    }

    //2.1根据Id查询勾选了哪些角色
    @RequestMapping("/findByUId")
    public Result findByUId(Integer id){
        try {
            List<Integer> list =userService.findByUId(id);
            return new Result(true,"回显数据成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"回显数据失败");
        }

    }

    //3.添加角色操作
    @RequestMapping("/addRole")
    public Result addRole( @RequestBody Integer[] checkitemIds, Integer user_id){
        try {
            userService.addRole(checkitemIds,user_id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加用户角色失败");
        }

        return new Result(true,"添加用户角色成功");

    }

    //4.首先进行用户的分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return userService.pageQuery(queryPageBean);

    }



}